package com.gec.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.SysUser;
import com.gec.model.vo.AssginRoleVo;
import com.gec.model.vo.SysUserQueryVo;
import com.gec.service.SysUserRoleService;
import com.gec.service.SysUserService;
import com.gec.system.common.Result;
import com.gec.system.util.MD5Helper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "用户管理控制器")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @ApiOperation("查询所有用户")
    @GetMapping("/findAllUser")
    public Result findAllUser(){
        List<SysUser> sysUsers = this.sysUserService.list(null);
        return Result.ok(sysUsers);
    }

    @ApiOperation("分页查询所有用户")
    @GetMapping("/{page}/{limit}")
    public Result findByPage(@PathVariable Long page, @PathVariable Long limit, SysUserQueryVo sysUserQueryVo){
        System.out.println("sysUserQueryVo.getDeptId() = " + sysUserQueryVo.getDeptId());
        IPage<SysUser> iPage = new Page<>(page,limit);
        iPage =this.sysUserService.selectPage(iPage, sysUserQueryVo);
        return Result.ok(iPage);
    }

//    @ApiOperation("添加用户")
//    @PostMapping("/addUser")
//    public Result addUser(@RequestBody SysUser sysUser){
//        boolean save = this.sysUserService.save(sysUser);
//        if(save){
//            return Result.ok();
//        }else{
//            return Result.fail();
//        }
//    }

    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody SysUser sysUser){
        String passwordMD5= MD5Helper.encrypt(sysUser.getPassword());
        sysUser.setPassword(passwordMD5);
        boolean save = this.sysUserService.save(sysUser);
        if(save){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @ApiOperation("逻辑删除单个用户")
    @DeleteMapping("/deleteUserByiD/{id}")
    public Result deleteUser(@PathVariable Long id){
        boolean byId = this.sysUserService.removeById(id);
        if(byId){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("查询单个用户")
    @GetMapping("/selectUserById/{id}")
    public Result selectUserById(@PathVariable Long id){
        SysUser byId = sysUserService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation("修改用户")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody SysUser sysUser){
        String passwordMD5= MD5Helper.encrypt(sysUser.getPassword());
        sysUser.setPassword(passwordMD5);
        boolean id = this.sysUserService.updateById(sysUser);
        if(id){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @ApiOperation("逻辑批量删除")
    @DeleteMapping("/deleteIds")
    public Result deleteIds(@RequestBody List<Long> ids){
        boolean byIds = this.sysUserService.removeByIds(ids);
       if(byIds){
           return Result.ok();
       }else{
           return Result.fail();
       }
    }

    @ApiOperation("修改用户状态")
    @PostMapping("/updatesysUser")
    public Result updateStatus(@RequestBody SysUser sysUser){
        System.out.println("sysUser:"+sysUser);
        boolean byId = sysUserService.updateById(sysUser);
        System.out.println("byId:"+byId);
        return  Result.ok();
    }

    @ApiOperation("根据用户id获取角色列表")
    @GetMapping("/toAssign/{userId}")
    public Result userfindRole(@PathVariable Long userId){
        Map<String,Object> sysRole = sysUserRoleService.getRolesByUserId(userId);
        System.out.println("sysRole = " + sysRole);
        return Result.ok(sysRole);
    }

    @ApiOperation("给用户分配角色")
    @PostMapping("/doAssign")
    public Result UsergetRole(@RequestBody AssginRoleVo assginRoleVo){
        sysUserRoleService.doAssign(assginRoleVo);
        return  Result.ok();
    }
}
